package tcln.gxtx.client.widget.grid.renderer;

import tcln.gxtx.client.utils.assertion.GwtAssert;
import tcln.gxtx.client.widget.treegrid.utils.TreeGridModelDataFormatter;

import com.extjs.gxt.ui.client.data.ModelData;

/**
 * Default implementation for gridcell renderer which just renders a cell as a normal text.
 * 
 * @param <M>
 *            Tree model data.
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class DefaultGridCellRenderer<M extends ModelData> extends AbstractGridCellRenderer<M> {

    protected TreeGridModelDataFormatter<M> modelDataFormatter;

    /**
     * Constructs this renderer with a formatter for model data.
     * 
     * @param modelDataFormatter
     */
    public DefaultGridCellRenderer(TreeGridModelDataFormatter<M> modelDataFormatter) {
        GwtAssert.ARGUMENTS.notNull(modelDataFormatter, "Formatter for model data must not be null");
        this.modelDataFormatter = modelDataFormatter;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String formatModelData(final M model, final String property) {
        if (modelDataFormatter != null) {
            return modelDataFormatter.format(model, property);
        }

        // otherwise, use toString for formatting
        Object propertyData = model.get(property);
        if (propertyData != null) {
            return propertyData.toString();
        }
        return "";
    }
}
